package com.jcl.recruit.server.elastic;

import com.jcl.core.common.page.PageResult;
import com.jcl.core.resp.ApiResponse;
import com.jcl.recruit.searchresume.model.SearchResumeRecord;
import com.jcl.recruit.server.elastic.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ElasticsearchServerLogic
 * @Description TODO
 * @Author yyliu
 * @Date 2018/11/21 9:17
 * @Version 1.0
 **/
@Component
public class SearchResumeServer {

    @Autowired
    private ElasticsearchServerProxy elasticsearchServerProxy;


    public PageResult<Map<String, Object>> selectSearchResume(String keyword,
                                                              String education,
                                                              String city,
                                                              Integer workYear,
                                                              Integer curPage) {

        SearchParamEntity entity = new SearchParamEntity();
        List<SearchResumeRecord> list = new ArrayList<>();
        SearchResumeRecord resumeRecord = new SearchResumeRecord();
        list.add(resumeRecord);
        resumeRecord.setWorkYear(workYear);
        resumeRecord.setKeyword(keyword);
        resumeRecord.setEducation(education);
        resumeRecord.setCity(city);

        entity.setResumeRecords(list);
        entity.setCurPage(curPage);
        return resumePageList(entity);
    }

    public PageResult<Map<String, Object>> resumePageList(SearchParamEntity entity) {
        ApiResponse<PageInfo> response = elasticsearchServerProxy.resumePageList(entity);
        PageInfo pageInfo = response.getData();
        PageResult<Map<String, Object>> pageResult = new PageResult<>();
        pageResult.setData(pageInfo.getData());
        pageResult.setRowSize(pageInfo.getRowSize());
        pageResult.setCurPage(pageInfo.getCurPage());
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    public SearchResumeResultEntity findEntity(Integer id) {
        ApiResponse<SearchResumeResultEntity> response = elasticsearchServerProxy.findEntity(id);
        return response.getData();
    }

    public SearchResumeSimpleResultEntity findSimpleEntity(Integer id) {
        ApiResponse<SearchResumeSimpleResultEntity> response = elasticsearchServerProxy.findSimpleEntity(id);
        return response.getData();
    }

    public SearchResumeBasicEntity findResumeBasicEntity(Integer id) {
        ApiResponse<SearchResumeBasicEntity> response = elasticsearchServerProxy.findResumeBasicEntity(id);
        return response.getData();
    }

    public Map<String, Object> downloadResume(Integer resumeId, String corpCode, String tenantId) {

        SearchDownloadParamEntity entity = new SearchDownloadParamEntity(corpCode, tenantId, resumeId);
        ApiResponse<Map<String, Object>> response = elasticsearchServerProxy.downloadResume(entity);

        if (ApiResponse.TYPE_ERROR.equals(response.getType())) {
            return null;
        }

        return response.getData();
    }

    public List<Map<String, String>> getCountBySources(Boolean today) {
        ApiResponse<List<Map<String, String>>> response = elasticsearchServerProxy.getCountBySources(today);
        return response.getData();
    }
}
